import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { CanDeactivateGuardService } from '../core/guards/can-deactivate-guard.service';
import { CrisisCenterHomeComponent } from './crisis-center-home.component';
import { CrisisCenterComponent } from './crisis-center.component';

// Two critical points
// The router's Resolve interface is optional. The CrisisDetailResolver doesn't inherit from a base class. 
// The router looks for that method and calls it if found.
// Rely on the router to call the resolver. Don't worry about all the ways that the user could navigate away. 
// That's the router's job. Write this class and let the router take it from there.
import { CrisisDetailResolverService } from './crisis-detail-resolver.service';

import { CrisisDetailComponent } from './crisis-detail.component';
import { CrisisListComponent } from './crisis-list.component';

const routes: Routes = [
    {
        path: '',
        component: CrisisCenterComponent,
        children: [
            {
                path: '',
                component: CrisisListComponent,
                children: [
                    {
                        // The Crisis Detail route is a child of the Crisis List. Since the router reuses components by default, 
                        // the Crisis Detail component will be re-used as you select different crises. In contrast, back in the Hero 
                        // Detail route, the component was recreated each time you selected a different hero.
                        path: ':id',
                        component: CrisisDetailComponent,
                        canDeactivate: [CanDeactivateGuardService],
                        resolve: {
                            crisis: CrisisDetailResolverService
                        }
                    },
                    {
                        path: '',
                        component: CrisisCenterHomeComponent
                    }
                ]
            }
        ]
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule],
    providers: [CrisisDetailResolverService]
})
export class CrisisCenterRoutingModule { }